package cn.edu.jxau.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import cn.edu.jxau.domain.notification;
import cn.itcast.jdbc.TxQueryRunner;

public class NotificationDao {
	private QueryRunner qr = new TxQueryRunner();

	/**
	 * 添加推送的资讯进数据库
	 * 
	 * @param NotificationDao
	 */
	public void addNotificationDao(notification notification) {
		String sql = "insert into notification values(?,?,?,?,?,?)";
		Object[] params = {notification.getNotificationID(), notification.getTitle(),notification.getContent(), notification.getLocation(),notification.getFlag(),notification.getUpdatetime()};
		try {
			qr.update(sql, params);
		} catch (SQLException e) {

		  System.out.println(e);

		}
	}
	
	/**
	 * 查询所有资讯  返回值：notification对象
	 */
	public List<notification> queryNotification() {
		String sql = "select * from notification";

		try {
			return  qr.query(sql,new BeanListHandler<notification>(notification.class));
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	

	/**
	 * 根据资讯ID查询资讯   返回值：notification对象
	 */
	public notification findByID(String ID) {
		String sql = "select * from notification where notificationId=?";

		try {
			return qr.query(sql, new BeanHandler<notification>(notification.class), ID);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}
	
	/**
	 * 根据ID删除资讯
	 */
	public void delByID(String notificationID){
		String sql="delete from notification where notificationId=?";
		try {
			qr.update(sql, notificationID);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
